package 比屋作业.day15;
import java.util.*;
import java.util.regex.Pattern;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class D15_1 {
    public static void main(String[] args) {
        UserSystem userSystem=new UserSystem();
        Scanner scanner=new Scanner(System.in);
        while(true){
            System.out.println("\n====用户系统====");
            System.out.println("1.注册");
            System.out.println("2.登录");
            System.out.println("3.退出");
            System.out.println("请选择您想进行的操作：");
            int choice=scanner.nextInt();
            switch (choice){
                case 1:userSystem.resister();
                        break;
                case 2:userSystem.logIn();
                        break;
                case 3:
                    System.out.println("感谢使用！");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效的选择请重新输入！");
            }
        }
    }
}
class User{
    private String Username;
    private String Password;
    private LocalDateTime localDateTime;
    private LocalDateTime lonIntime;
    public User(String Username,String Password,LocalDateTime localDateTime){
        this.Username=Username;
        this.Password=Password;
        this.localDateTime=localDateTime;
    }
    public void setUsername(String Username) {
        this.Username=Username;
    }
    public void setPassword(String Password){
        this.Password=Password;
    }
    public void setLocalDateTime(LocalDateTime localDateTime) {
        this.localDateTime = localDateTime;
    }
    public void setLonIntime(LocalDateTime lonIntime) {
        this.lonIntime=lonIntime;
    }

    public String getUsername() {
        return Username;
    }
    public String getPassword() {
        return Password;
    }
    public LocalDateTime getLocalDateTime() {
        return localDateTime;
    }
    public LocalDateTime getLonIntime() {
        return lonIntime;
    }
}
class UserSystem{
    Scanner scanner=new Scanner(System.in);
    HashMap<String,User> hashMap=new HashMap<>();
    public void resister() {
        String Username=null;
        String Password=null;
        boolean testPassword=false;
        boolean testUsername=false;
           while (!testUsername) {
               System.out.println("请输入注册用户名：（不能超过16位并且只能⽤⼤⼩写字⺟下划线及数字）");
                Username = scanner.nextLine();
               if (Pattern.matches("^[a-zA-Z0-9_]{1,16}$",Username)){
                    if (hashMap.containsKey(Username)){
                        System.out.println("用户名已存在！");
                    }
                    else{
                        testUsername=true;
                    }
               }
               else {
                   System.out.println("用户名不合法！");
               }
           }
           while (!testPassword){
               System.out.println("请输入注册密码：（必须包含大写字母和特殊符号）");
                Password=scanner.nextLine();
               if(Pattern.matches("^(?=.*[A-Z])(?=.*[^a-zA-Z0-9]).{8,}$",Password)){
                   testPassword=true;
               }
               else {
                   System.out.println("密码不合法，请重新输入！");
               }
           }
        LocalDateTime now=LocalDateTime.now();
        User newUser=new User(Username,Password,now);
        hashMap.put(Username,newUser);
        DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println("注册成功！");
        System.out.println("用户名为："+Username);
        System.out.println("注册时间为："+now.format(formatter));
    }
    public void logIn(){
        System.out.println("请输入用户名：");
        String Username=scanner.nextLine();
        System.out.println("请输入密码：");
        String Password=scanner.nextLine();
        User user=hashMap.get(Username);
        if (Username!=null&&user.getPassword().equals(Password)){
            LocalDateTime now=LocalDateTime.now();
            user.setLonIntime(now);
            DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            System.out.println("登陆成功！");
            System.out.println("欢迎回来!"+Username);
            System.out.println("上次登录时间："+(user.getLonIntime()== null ?"首次登录":user.getLonIntime().format(formatter)));
        }
        else {
            System.out.println("登陆失败，用户名或密码错误！");
        }
    }
}

